<?php include 'header.php'; ?>
<?php if(!isset($_SESSION['user'])):  ?>
			<meta http-equiv="refresh" content="0;URL=login.php">
		<?php else: ?>
	<script type="text/javascript">
		function fillHiddenField(val) { //This function will handle sorting the table. Fills a hidden form attribute that submits with the search query.
			document.getElementById("sort").value=val;
			document.getElementById("search").submit();
			return true;
		}
	</script>
 
	<?php
		$order = "ASC";
		
		if ($_POST["sort"] != "") {
			$sort = htmlspecialchars($_POST["sort"]);
			//Switches the sorting order if sorting by date (to sort by most recent first)
			if ($sort == "submit_date")
				$order = "DESC";
		}
		else { //If there is no sort selected, it will automatically sort by submission date. 
			$sort = "submit_date";
			$order = "DESC";
		}

		$con = mysqli_connect ("db.cse.unt.edu", 'acmrepo', 'B8cpU6GBx9qJJmN8', 'acmrepo');

		if (mysqli_connect_errno()) {
		  echo "Failed to connect to MySQL: " . mysqli_connect_error();
		  require "footer.php";
		  die();
		}

		if(empty($_POST["term"])) {
			$query = "SELECT abstractID, link, first_name, last_name, dept_name, univ_name, title, username, submit_date, uploadedAbstractID FROM author natural join abstract 
				order by " . $sort . " " . $order;			
		}
		else {  //This is the query to handle searching. 
			$query = "SELECT abstractID, link, first_name, last_name, dept_name, univ_name, title, username, keywords, submit_date, uploadedAbstractID FROM author natural join abstract WHERE " 
				. $_POST['method'] . " LIKE '%" . $_POST['term'] . "%' order by " . $sort . " " . $order;
		}
		
		$result = mysqli_query($con, $query);
	?>

	<h3> Search </h3>
	<form id="search" name="search" method="POST" action="">
		Search Terms: <input type='text' name='term' value=<?php 
								if (!empty($_POST["term"]))
									echo '"' . htmlspecialchars($_POST["term"]) . '"';
								else
									echo '""'; ?> /> <br>
		Search By: <select name="method">
						<option value="keywords">Key Word</option>
						<option value="title">Title</option>
						<option value="first_name">First Name</option>
						<option value="last_name">Last Name</option>
						<option value="dept_name">Department Name</option>
						<option value="univ_name">University Name</option>
					</select>
		<input type='hidden' id='sort' name="sort" value=''/>
		<br><input type="submit" name="search" value="search"/><br><br>
	</form>

	<!--This is the first row of the table displaying the query results. Each column is a link to a re-ordering of the table. -->
	<table class="hoverTable">
		<tr>
				<th><a href = '#' onclick='fillHiddenField("submit_date"); return false;'>Submission Date</a></th>
				<th><a href = '#' onclick='fillHiddenField("first_name"); return false;'>First Name</a></th>
				<th><a href = '#' onclick='fillHiddenField("last_name"); return false;'>Last Name</a></th>
				<th><a href = '#' onclick='fillHiddenField("dept_name"); return false;'>Department Name</a></th>
				<th><a href = '#' onclick='fillHiddenField("univ_name"); return false;'>University Name</a></th>
				<th>Title</th>
				<th>Download Abstract</td>
				<th>Full Dissertation</th></tr>
	<?php 
		while ($row = mysqli_fetch_array($result)){
			echo "<tr><td>" . date('m/j/Y', strtotime($row['submit_date'])) .
				"</td><td>" . $row['first_name'] . 
				"</td><td>" . $row['last_name'] . 
				"</td><td>" . $row['dept_name'] . 
				"</td><td>" . $row['univ_name'] .
				"</td><td>" . "<a href = displaySearch.php?search=" . $row['username'] . "&ID=" . $row['abstractID'] . ">" . $row['title'] . "</a>" . "</td>" .
				"</td><td>";
				if($row['uploadedAbstractID'] > 0)
					echo "<a href = getFile.php?id=" . $row['uploadedAbstractID'] . "><img src='images/download-icon.png' height=24px width=24px></a></td>"; 
				else
					echo "<img src='images/red-x.png' height=24px width=24px></td>";
				if (!empty($row['link']))
					echo "</td><td><a href = " . $row['link'] . " target='_blank'><img src='images/linkicon.png' height=24px width=24px></a></td>";
				else
					echo "</td><td><img src='images/red-x.png' height=24px width=24px></td>";
				if (in_array($_SESSION['user'], $admins))
					echo "<td><a href = deleteAbstract.php?abstract=" . $row['abstractID'] . "><img src='images/trashbin.png' height=24px width=24px></a></td></tr>";
		}
		echo "</table>";
		mysqli_close($con);
	?>
	<?php endif; ?>
	<?php include 'footer.php'; ?>
